/**
 * Created by Feng on 2016/3/25.
 */

import {provide, Directive, forwardRef, Input} from '@angular/core';
import {Control, NG_VALIDATORS, Validator} from '@angular/common';
/* beautify ignore:end */



@Directive({
    selector: '[max-size]',
    providers: [provide(NG_VALIDATORS, {useExisting: MaxSizeValidator, multi: true})]
})

export class MaxSizeValidator implements Validator {
    @Input('max-size') size: string; 
    validate(c: Control): {[key: string]: any} {
        let value = c.value;
        if(value == null )
            return null;
        if(value.replace(/[^\x00-\xff]/g, "**").length <= parseInt(this.size)){
            return null;
        }
        return {"maxSize":true};
    }
}